﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcCms.Web.Extensions
{
    public static class ModelExtensions
    {
        public static bool HasValue(this string value)
        {
            return !string.IsNullOrEmpty(value) && value.Trim().Length > 0;
        }

        public static void Merge(this ModelStateDictionary controllerModelState, ModelStateDictionary serviceModelState)
        {
            Guard.AgainstNullParameter(controllerModelState, "modelState");
            Guard.AgainstNullParameter(controllerModelState, "dictionary");

            foreach (var item in serviceModelState)
            {
                foreach (var keyItem in item.Value.Errors)
                {
                    controllerModelState.AddModelError(item.Key, keyItem.ErrorMessage);
                }
            }
        }
    }

    public static class Guard
    {
        public static void AgainstNullParameter(object parameter, string parameterName)
        {
            if (parameter == null) throw new ArgumentNullException(parameterName);
        }
    } 
}